package controller

import (
	"encoding/json"
	"github.com/go-openapi/runtime"
	"github.com/go-openapi/runtime/middleware"
	"mlss-cc-a/common"
	"mlss-cc-a/models"
	"mlss-cc-a/restapi/operations/alarms"
	service "mlss-cc-a/service/alarm"
	"net/http"
)

func GetAlarm(param alarms.GetAlarmsParams) middleware.Responder {
	host := param.HTTPRequest.Host
	println(host)

	err := common.CheckForAlarm(param)
	if err != nil {
		return ResponderFunc(err)
	}

	err, strings := service.AlarmService.GetClusterListService(host)
	if err != nil {
		return ResponderFunc(err)
	}

	marshal, _ := json.Marshal(strings)
	println(string(marshal))
	//return alarms.NewGetDefault(123)
	return alarms.NewGetOK()
}

func ResponderFunc(err error) middleware.ResponderFunc {
	return func(w http.ResponseWriter, _ runtime.Producer) {
		s := err.Error()
		payload, _ := json.Marshal(models.Item{
			ID:          100,
			Description: &s,
			Completed:   true,
		})
		w.Write(payload)
	}
}
